package day17_Inet.demo08_TCP;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;

public class Server_TCP {
    public static void main(String[] args) throws IOException {
        Scanner scanner = new Scanner(System.in);
        ServerSocket ss = new ServerSocket(8888);
        Socket accept = ss.accept();
        while (true){
            //接收数据
            InputStream is = accept.getInputStream();
            byte[] bytes = new byte[1024];
            int i = is.read(bytes);
            String s1 = new String(bytes, 0, i);
            System.out.println("接收的数据为"+s1);
            if (s1.equals("bye")){
                break;
            }
            //发送数据
            OutputStream os = accept.getOutputStream();
            String s = scanner.next();
            os.write(s.getBytes());
            if (s.equals("bye")){
                break;
            }
        }
        ss.close();
    }
}
